package org.jivesoftware.smackx.workgroup.ext.macros;

import java.io.StringReader;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smack.util.StringUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

public class Macros extends IQ
{
  public static final String ELEMENT_NAME = "macros";
  public static final String NAMESPACE = "http://jivesoftware.com/protocol/workgroup";
  private boolean personal;
  private MacroGroup personalMacroGroup;
  private MacroGroup rootGroup;

  public String getChildElementXML()
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<").append("macros").append(" xmlns=\"").append("http://jivesoftware.com/protocol/workgroup").append("\">");
    if (isPersonal())
      localStringBuilder.append("<personal>true</personal>");
    if (getPersonalMacroGroup() != null)
    {
      localStringBuilder.append("<personalMacro>");
      localStringBuilder.append(StringUtils.escapeForXML(getPersonalMacroGroup().toXML()));
      localStringBuilder.append("</personalMacro>");
    }
    localStringBuilder.append("</").append("macros").append("> ");
    return localStringBuilder.toString();
  }

  public MacroGroup getPersonalMacroGroup()
  {
    return this.personalMacroGroup;
  }

  public MacroGroup getRootGroup()
  {
    return this.rootGroup;
  }

  public boolean isPersonal()
  {
    return this.personal;
  }

  public void setPersonal(boolean paramBoolean)
  {
    this.personal = paramBoolean;
  }

  public void setPersonalMacroGroup(MacroGroup paramMacroGroup)
  {
    this.personalMacroGroup = paramMacroGroup;
  }

  public void setRootGroup(MacroGroup paramMacroGroup)
  {
    this.rootGroup = paramMacroGroup;
  }

  public static class InternalProvider
  implements IQProvider
  {
    public IQ parseIQ(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      Macros localMacros = new Macros();
      int i = 0;
      while (true)
      {
        int j;
        label61: 
        do
          while (true)
          {
            do
            {
              if (i != 0)
                return localMacros;
              j = paramXmlPullParser.next();
              if (j != 2)
                break label61;
            }
            while (!(paramXmlPullParser.getName().equals("model")));
            localMacros.setRootGroup(parseMacroGroups(paramXmlPullParser.nextText()));
          }
        while ((j != 3) || (!(paramXmlPullParser.getName().equals("macros"))));
        i = 1;
      }
    }

    public Macro parseMacro(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      Macro localMacro = new Macro();
      int i = 0;
      while (true)
      {
        int j;
        label151: 
        do
          while (true)
          {
            do
              while (true)
              {
                while (true)
                {
                  while (true)
                  {
                    if (i != 0)
                      return localMacro;
                    j = paramXmlPullParser.next();
                    if (j != 2)
                      break label151;
                    if (!(paramXmlPullParser.getName().equals("title")))
                      break;
                    paramXmlPullParser.next();
                    localMacro.setTitle(paramXmlPullParser.getText());
                  }
                  if (!(paramXmlPullParser.getName().equals("description")))
                    break;
                  localMacro.setDescription(paramXmlPullParser.nextText());
                }
                if (!(paramXmlPullParser.getName().equals("response")))
                  break;
                localMacro.setResponse(paramXmlPullParser.nextText());
              }
            while (!(paramXmlPullParser.getName().equals("type")));
            localMacro.setType(Integer.valueOf(paramXmlPullParser.nextText()).intValue());
          }
        while ((j != 3) || (!(paramXmlPullParser.getName().equals("macro"))));
        i = 1;
      }
    }

    public MacroGroup parseMacroGroup(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      MacroGroup localMacroGroup = new MacroGroup();
      int i = 0;
      while (true)
      {
        int j;
        label103: 
        do
          while (true)
          {
            do
            {
              if (i != 0)
                return localMacroGroup;
              j = paramXmlPullParser.next();
              if (j != 2)
                break label103;
              if (paramXmlPullParser.getName().equals("macrogroup"))
                localMacroGroup.addMacroGroup(parseMacroGroup(paramXmlPullParser));
              if (paramXmlPullParser.getName().equals("title"))
                localMacroGroup.setTitle(paramXmlPullParser.nextText());
            }
            while (!(paramXmlPullParser.getName().equals("macro")));
            localMacroGroup.addMacro(parseMacro(paramXmlPullParser));
          }
        while ((j != 3) || (!(paramXmlPullParser.getName().equals("macrogroup"))));
        i = 1;
      }
    }

    public MacroGroup parseMacroGroups(String paramString)
      throws Exception
    {
      MacroGroup localMacroGroup = null;
      XmlPullParser localXmlPullParser = XmlPullParserFactory.newInstance().newPullParser();
      localXmlPullParser.setInput(new StringReader(paramString));
      int i = localXmlPullParser.getEventType();
      while (true)
      {
        do
        {
          if (i == 1)
            return localMacroGroup;
          i = localXmlPullParser.next();
        }
        while ((i != 2) || (!(localXmlPullParser.getName().equals("macrogroup"))));
        localMacroGroup = parseMacroGroup(localXmlPullParser);
      }
    }
  }
}